package com.have.blog.admin.service.impl;

import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.ToDoDto;
import com.have.blog.admin.service.AdminTodoService;
import com.have.blog.admin.utils.SecurityUtil;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.config.configs.jwt.Audience;
import com.have.blog.model.mapper.TodoMapper;
import com.have.blog.model.models.Todo;
import com.have.blog.model.models.TodoExample;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@SuppressWarnings("all")
public class AdminTodoServiceImpl implements AdminTodoService {

    @Autowired
    private TodoMapper todoMapper;
    @Autowired
    private Audience audience;

    @Override
    public List<ToDoDto> getList(ToDoDto todoDto) {
        Integer adminUid = SecurityUtil.getUserId();

        TodoExample example = new TodoExample();
        example.setOrderByClause("create_time desc");
        TodoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(todoDto.getKeyword()) && !StringUtils.isEmpty(todoDto.getKeyword().trim())) {
            criteria.andTextLike("%"+ todoDto.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andAdminUidEqualTo(adminUid);
        List<Todo> todoList = todoMapper.selectByExample(example);
        List<ToDoDto> resultList = MyBeanUtils.copyListProperties(todoList, ToDoDto::new);
        return resultList;
    }

    @Override
    public Wrapper addTodo(ToDoDto todoDto) {
        Integer adminUid = SecurityUtil.getUserId();

        Todo todo = new Todo();
        todo.setText(todoDto.getText());
        todo.setDone(0);
        todo.setStatus(BlogStatus.ENABLE.getStatus());
        todo.setAdminUid(adminUid);
        int result = todoMapper.insert(todo);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_FAIL);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editTodo(ToDoDto todoDto) {
        Todo todo = todoMapper.selectByPrimaryKey(todoDto.getUid());
        Integer adminUid = SecurityUtil.getUserId();
        if (!todo.getAdminUid().equals(adminUid)) {
            return Wrapper.forbidden();
        }
        todo.setText(todoDto.getText());
        todo.setDone(todoDto.getDone());
        int result = todoMapper.updateByPrimaryKeySelective(todo);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteTodo(ToDoDto todoDto) {
        Integer adminUid = SecurityUtil.getUserId();

        Todo todo = todoMapper.selectByPrimaryKey(todoDto.getUid());

        if (!todo.getAdminUid().equals(adminUid)) {
            return Wrapper.forbidden();
        }
        todo.setStatus(BlogStatus.DISABLE.getStatus());
        todo.setUpdateTime(new Date());
        int result = todoMapper.updateByPrimaryKeySelective(todo);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }

    @Override
    public Wrapper editBatchTodo(List<Integer> uids) {
        return  Wrapper.success();
    }
}
